class FriendshipObserver < ActiveRecord::Observer
  def after_create(friendship)
    user = User.find(friendship.user_id)
    friend = User.find(friendship.friend_id)
    if user.is_email_notify == 1
      UserNotifier.deliver_new_friendship(user, friend)
    end
    
    #发一封站内邮件通知
    Message.transaction do
      user_admin = User.find_by_login("admin") 
      @message = Message.new
      @message.title = "#{user.display_name}把你加为好友了"
      @message.content = "#{user.display_name}把你加为好友了, #{user.display_name}的主页是http://www.ninzher.com/users/#{user.login}，去看看吧"
      @message.save!
      r_user_message = UserMessage.new(:user => friend, :message => @message, :from_id => user_admin.id, :to_id => friend.id, :box => 'r', :is_read => 'f')
      s_user_message = UserMessage.new(:user => user_admin, :message => @message, :from_id => user_admin.id, :to_id => friend.id, :box => 's', :is_read => 't')
      r_user_message.save!
      s_user_message.save!
    end
    
  end
end
